package com.honour.controller.interfaced;


import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.service.interfaced.ProductInManager;
import com.honour.service.interfaced.ShippingInManager;
import com.honour.service.interfaced.SortInManager;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.DateUtil;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;
import com.honour.service.interfaced.impl.AdvertisementInService;





/**
 @Function
 @Author 于泓磊
 @Date   2017年10月21日
*/

@Controller
@RequestMapping(value="/appshipin")
public class ShippingInController extends BaseController{
	
	@Resource(name="shipinService")
	private ShippingInManager shipinService;
	
	@Resource(name="sortinService")
	private SortInManager sortinService;
	
	@Resource(name="advertisementInService")
	private AdvertisementInService advertisementInService;
	
	@Resource(name="productInService")
	private ProductInManager productInService;
	
	
	/**
	 * 获得当前id商品的具体信息
	 * @Author 于泓磊
	 * @date   2017年10月23日
	 * @return
	 */
	@RequestMapping(value="/shipdetails")
	@ResponseBody
	public Object shipDetails(){
		logBefore(logger, "查询商品详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPDT", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
					pd=shipinService.getShipDetailsById(pd);
					if(pd==null){
						map.put("result", "02");
						return AppUtil.returnObject(new PageData(), map);
					}
					ToolsUtil.putImgstoArray(pd, "PRODETAILSIMG");
					ToolsUtil.putImgstoArray(pd, "PRODUCTIMG");
				    //商品的评价列表
				    List<PageData> evaList = shipinService.getProductEvaList(pd);
				    //获取店铺信息
				    PageData business = productInService.getBusinessByProId(pd);
				    map.put("evaList", evaList);
				    map.put("business", business);
					map.put("pd", pd);
					//返回01为查询成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取所有商品列表
	 * @Author 于泓磊
	 * @date   2017年10月23日
	 * @return
	 */
	@RequestMapping(value="/shiplistall")
	@ResponseBody
	public Object shipListAll(){
		logBefore(logger, "获取所有商品列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPAll", pd.getString("FKEY"))){	//检验请求key值是否合法
				    List<PageData> list = shipinService.getShipList(pd);
				    for (PageData p : list) {
				    	//调用工具类将数据库的图片属性变为数组
						ToolsUtil.putImgstoArray(p,"PRODUCTIMG");
					}
					map.put("pd", list);
					//返回01为查询成功并返回
					result =  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得商品类别下商品品牌列表
	 * @Author 于泓磊
	 * @date   2017年10月25日
	 * @return
	 */
	@RequestMapping(value="/shipbrandiconlist")
	@ResponseBody
	public Object shipBrandIconList(){
		logBefore(logger, "获取商品品牌列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPBRIC", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipBrandIconList", pd)){	//检查参数
				List<PageData> list = shipinService.getBrandIconList(pd);
				map.put("pd", list);
					//返回01为查询成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	

	/**
	 * 获得商品类别列表
	 * @Author 于泓磊
	 * @date   2017年10月25日
	 * @return
	 */
	@RequestMapping(value="/shipbrandlist")
	@ResponseBody
	public Object shipBrandList(){
		logBefore(logger, "获得商品类别列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPBR", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = shipinService.getBrandList(pd);
				map.put("pd", list);
					//返回01为查询成功并返回
					result =  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 尊购首页各种专区接口
	 * @Author 于泓磊
	 * @date   2017年10月25日
	 * @return
	 */
	@RequestMapping(value="/shipprefecture")
	@ResponseBody
	public Object shipPrefecture(){
		logBefore(logger, "尊购首页各种专区接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPPRE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipBrandIconList", pd)){	//检查参数
				List<PageData> list = shipinService.getPrefecture(pd);
				map.put("pd", list);
					//返回01为查询成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 尊购首页模糊查询
	 * @Author 于泓磊
	 * @date   2017年10月31日
	 * @return
	 */
	@RequestMapping(value="/shipdimquery")
	@ResponseBody
	public Object shipDimQuery(){
		logBefore(logger, "尊购首页模糊查询接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPDIMQ", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("allOrder", pd)){	//检查参数
				List<PageData> list = shipinService.shipDimQuery(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
					//返回01为查询成功并返回
				result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 炫购收藏列表
	 * @Author 于泓磊
	 * @date   2017年10月31日
	 * @return
	 */
	@RequestMapping(value="/shipcollect")
	@ResponseBody
	public Object shipCollect(){
		logBefore(logger, "炫购收藏列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPCOL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = shipinService.shipCollectList(pd);
					if (list.size()!=0) {
						map.put("pd", list);
					}
					//返回01为查询成功并返回
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 添加商品到炫购收藏
	 * @Author 于泓磊
	 * @date   2017年10月31日
	 * @return
	 */
	@RequestMapping(value="/shipcollectadd")
	@ResponseBody
	public Object shipCollectAdd(){
		logBefore(logger, "添加商品到炫购收藏接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPCOLADD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipCollectAdd", pd)){	//检查参数
				pd.put("COLLECT_ID", this.getSnowId());
				shipinService.shipCollectAdd(pd);
				//返回01为查询成功并返回
				result = "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 从炫购收藏删除商品
	 * @Author 于泓磊
	 * @date   2017年10月31日
	 * @return
	 */
	@RequestMapping(value="/shipcollectdel")
	@ResponseBody
	public Object shipCollectDel(){
		logBefore(logger, "删除接口炫购收藏");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPCOLDEL", pd.getString("FKEY"))){	//检验请求key值是否合法
				shipinService.shipCollectDel(pd);
				//返回01为查询成功并返回
				result = "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据上架状态获取轮播图
	 * @Author zhaozhenhua
	 * @date   2017年11月20日
	 * @return
	 */
	@RequestMapping(value="/scarouselfigure")
	@ResponseBody
	public Object findCarouselByStatus(){
		logBefore(logger, "根据状态获取轮播图");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SCAROUSELFIGURE", pd.getString("FKEY"))){	//检验请求key值是否合法
				PageData p = shipinService.findCarouselByStatus(pd);
				if (!"".equals(p.get("SHOWIMAGES"))&&p.get("SHOWIMAGES")!=null) {
					ToolsUtil.putImgstoArray(p, "SHOWIMAGES");
				}
				if (p!=null) {
					map.put("pd", p);
				}
			//返回01为添加成功并返回,02为轮播图都为下架状态
			result = (null == p) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 给商品添加用户评价
	 * @Author zhaozhenhua
	 * @date   2017年12月18日
	 * @return
	 */
	@RequestMapping(value="/sproductaddeva")
	@ResponseBody
	public Object sProductEvaAdd(){
		logBefore(logger, "给商品添加用户评价");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SPRODUCTADDEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sProductAddEva", pd)){	//检查参数
				pd.put("PRODUCTEVALUATE_ID", this.getSnowId());
				pd.put("EVALUATEDATE", DateUtil.getDay());
				if(pd.getString("EVALUATECOINT")!=null&&!"".equals(pd.getString("EVALUATECOINT"))){
					pd.put("EVALUATECOINT",Double.parseDouble((String) pd.get("EVALUATECOINT")));
				}else {
					pd.put("EVALUATECOINT",0);
				}
				//用户是否匿名评价判断
				String NICKNAME = sortinService.getUserNickName(pd).getString("NICKNAME");
				if (pd.get("ISSHOW").equals("1")) {
					pd.put("NICKNAME", NICKNAME.subSequence(0, 1)+"****"+NICKNAME.subSequence(NICKNAME.length()-1, NICKNAME.length()));
				}else{
					pd.put("NICKNAME", NICKNAME);
				}
				shipinService.addProductEva(pd);
				//每当店铺有新评价，则更新评价的分数和评价数量
				PageData shipDetails = shipinService.getShipDetailsById(pd);
				double d = shipinService.getProductEvaMean(pd);
				shipDetails.put("PROEVALUATE", d);
				Integer EVALUATECOUNT = shipDetails.getInterger("EVALUATECOUNT");
				EVALUATECOUNT = EVALUATECOUNT+1;
				shipDetails.put("EVALUATECOUNT", EVALUATECOUNT);
				shipinService.updProEvaCount(shipDetails);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 尊购首页各种专区点击后返回的店家列表
	 * @Author zhaozhenhua
	 * @date   2018年1月10日
	 * @return
	 */
	@RequestMapping(value="/shipbusinesslist")
	@ResponseBody
	public Object shipBusinessList(){
		logBefore(logger, "尊购首页各种专区点击后返回的店家列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPPBUSINESS", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipBrandIconList", pd)){	//检查参数
				List<PageData> list = shipinService.getBusinessList(pd);
				for (PageData data : list) {
					ToolsUtil.putImgstoArray(data, "PRODUCTIMGS");
				}
				List<PageData> adList = advertisementInService.getAdByPosition(pd);
				List<PageData> brandIconList = shipinService.getBrandIconList(pd);
				map.put("brandIconList", brandIconList);
				map.put("adList", adList);
				map.put("pd", list);
				result ="01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得尊购首页专区列表
	 * @Author zhaozhenhua
	 * @date   2018年1月26日
	 * @return
	 */
	@RequestMapping(value="/getfecturelist")
	@ResponseBody
	public Object getFectureList(){
		logBefore(logger, "获得尊购首页专区列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("FECTURELIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = shipinService.getBrandList(pd);
				for (int i = 0; i < list.size(); i++) {
					 if(list.get(i).getString("BRAND_ID").equals("0e6184d78b374cfd8e97f9da5e34a835")){
						list.remove(i);
					 }if(list.get(i).getString("BRAND_ID").equals("175fb2fb785b4fdba9d9b9022d0575c4")){
							list.remove(i);
					 }if(list.get(i).getString("BRAND_ID").equals("fc0c4cb21de64f83a0ae146958d42cfe")){
							list.remove(i);
					 }if(list.get(i).getString("BRAND_ID").equals("668b21fc68a44080899cfd840107af22")){
							list.remove(i);
					 }if(list.get(i).getString("BRAND_ID").equals("3b067bd936ea4578a6eabec6b2e80a50")){
							list.remove(i);
					 }if(list.get(i).getString("BRAND_ID").equals("195f038a5e984ef18ea53d142586cc66")){
							list.remove(i);
					 }
				}
				for (PageData p : list) {
					if(p.getString("BRAND_ID").equals("a8824559f11742d6acf0b07604a55337")){
						p.put("BRANDBANE", "美酒专区");
					}else if(p.getString("BRAND_ID").equals("a34d1f14a4b7481e8284ad4ba97a496b")){
						p.put("BRANDBANE", "包袋专区");
					}else if(p.getString("BRAND_ID").equals("1b4ed4c57ef04933b97e8def48fc423a")){
						p.put("BRANDBANE", "手表专区");
					}else if(p.getString("BRAND_ID").equals("876c483370ab49adbec66938b265c90f")){
						p.put("BRANDBANE", "服饰专区");
					}else if(p.getString("BRAND_ID").equals("2df2c7e628b14341be1e2932cb377c82")){
						p.put("BRANDBANE", "首饰专区");
					}else if(p.getString("BRAND_ID").equals("3b067bd936ea4578a6eabec6b2e80a50")){
						p.put("BRANDBANE", "美妆专区");
					}else if(p.getString("BRAND_ID").equals("69f2077efb1d46d2aac7954d145286cc")){
						p.put("BRANDBANE", "户外专区");
					}
				}
				map.put("pd", list);
					//返回01为查询成功并返回
					result =  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
}
